var express = require("express");
var router = express.Router();
var utils = require('../utils/index')
var Article = require('../models/article.model')

/* GET home page. */
router.get("/", function (req, res, next) {
  res.send(utils.setSendResult());
  res.render("error", {
    message: "test",
    error: {
      status: "404",
      stack: "找不到当前路径",
    },
  });
});


router.get("/query", function (req, res, next) {
  const query = req.query;
  Article.findAll().then(result=>{
    res.send(utils.setSendResult(200, "查询成功", result));
  })
});

router.post("/add", function (req, res, next) {
    const params =  req.body;
    var article = Article.create(params);
    res.send(utils.setSendResult(200, "添加成功", article));
});

router.post("/edit", function (req, res, next) {
    const params = req.body;
    if(params['id']==undefined) return res.send(utils.setSendResult(400, "参数错误", {}));
    var article = Article.update(params);
    res.send(utils.setSendResult(200, "修改成功", article));
});

router.get("/delete", function (req, res, next) {
    const query = req.query;
    if(query['id']==undefined) return res.send(utils.setSendResult(400, "参数错误", {}));
    var article = Article.destroy({
        where:{
            id:query['id']
        }
    });
    res.send(utils.setSendResult(200, "删除成功", article));
});

module.exports = router;
